/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.form.bindings;

import java.io.Serializable;

import org.nuxeo.gwt.habyt.app.client.form.FormControl;
import org.nuxeo.gwt.habyt.templates.client.bindings.Helper;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Hidden;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class HiddenFactory extends AbstractControlFactory {

    public HiddenFactory() {
        super("hidden");
    }

    @Override
    protected FormControl createControl(Element element) {
        String name = Helper.getAttribute(element, "name");
        String value = Helper.getAttribute(element, "value");

        Hidden w = new Hidden();
        if (name != null) {
            w.setName(name);
        }
        if (value != null) {
            w.setValue(value);
        }
        return new FormControl(w) {
            @Override
            public String getName() {
                return ((Hidden) getWidget()).getName();
            }

            @Override
            public Serializable getValue() {
                return ((Hidden) getWidget()).getValue();
            }

            public void setValue(Serializable value) {
                ((Hidden) getWidget()).setValue(value == null ? ""
                        : value.toString());
            }
        };
    }

}
